package com.lashou.chunshen.lashoutuangou.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lashou.chunshen.lashoutuangou.bean.BaseBean;
import com.lashou.chunshen.lashoutuangou.bean.HomeBanner;
import com.lashou.chunshen.lashoutuangou.widget.verticalviewpager.VerticalViewPager;

/**
 * Created by Administrator on 2016/9/6.
 */

public class TopLinePageAdapter<T extends BaseBean> extends PagerAdapter {
    public T datas;
    Activity mActivity;
    TextView text;
    public OnClick onClick;
    VerticalViewPager verticalviewpager;

    @Override
    public int getCount() {
        if (datas != null) {
            //第一处修改，设置轮播最大值，等于无限循环
            return Integer.MAX_VALUE;
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public TopLinePageAdapter(Activity activity, T datas) {
        this.datas = datas;
        this.mActivity = activity;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        final int newPosition = position % (((HomeBanner) datas).getResults().getText_banner().size());
        text = new TextView(mActivity);
        //设置颜色textView
        String text_banner = ((HomeBanner) datas).getResults().getText_banner().get(newPosition).getTag();//标签
        String text_title = ((HomeBanner) datas).getResults().getText_banner().get(newPosition).getTitle();//title
        StringBuilder sb = new StringBuilder();
        sb.append("【");
        sb.append(text_banner);
        sb.append("】");
        sb.append(text_title);
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sb.toString());
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text_banner.length() + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        text.setText(spannableStringBuilder);
        //设置点击事件
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null) {
                    onClick.onClick(v, ((HomeBanner) datas).getResults().getText_banner().get(newPosition).getShare_info());
                }
            }
        });
        container.addView(text);
        return text;
    }


    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public void setViewpager(VerticalViewPager verticalviewpager) {
        this.verticalviewpager = verticalviewpager;
    }

    public void openAutoLoop() {
        final boolean[] isLooper = new boolean[1];
        //开启一个线程，用于循环
        new Thread(new Runnable() {
            @Override
            public void run() {
                isLooper[0] = true;
                while (isLooper[0]) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //更新UI
                            verticalviewpager.setCurrentItem(verticalviewpager.getCurrentItem() + 1);
                        }
                    });
                }

            }
        }).start();
    }

    public interface OnClick {
        void onClick(View view, BaseBean bean);
    }

}
